Imports System.Data.SqlClient
Public Class FormPurchaseEditor
     
    Private Sub FormPurchaseEditor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        UI.FormatDataGrid(Me.dgv)

        Me.txtPurchaseNo.Text = NoGenerator.GenerateNewPurchaseNo()

        Me.cboStore.DisplayMember = "StoreName"
        Me.cboStore.ValueMember = "StoreId"
        Me.cboStore.DataSource = Cmd.ExecuteDataTable("SELECT * FROM Store WHERE Active=1")

        Me.cboVendor.DisplayMember = "Vendor"
        Me.cboVendor.ValueMember = "VendorId"
        Me.cboVendor.DataSource = Cmd.ExecuteDataTable("SELECT VendorId,Vendor = VendorName+' ('+Phone+')' FROM Vendor WHERE Active=1")



    End Sub

    Private Sub txtProductCode_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtProductCode.KeyDown

        If e.KeyCode <> Keys.Enter Then
            Exit Sub
        End If


        Cmd.Parameters("@Code") = Me.txtProductCode.Text
        Dim dt = Cmd.ExecuteDataTable("SELECT * FROM Product WHERE ProductCode=@Code")

        If (dt.Rows.Count = 0) Then
            ClearLine()
            BrowseProduct()
            Exit Sub
        End If

        Dim row = dt.Rows(0)
        Me.txtProductId.Text = row("ProductId")
        Me.txtProductName.Text = row("ProductName")
        Me.txtPrice.Text = row("Price")
        Me.txtQty.Text = 1
        Me.txtAmount.Text = txtPrice.Text
    End Sub
     
    Private Sub ClearLine()
        Me.txtProductId.Text = ""
        Me.txtProductName.Text = ""
        Me.txtQty.Text = ""
        Me.txtPrice.Text = ""
        Me.txtAmount.Text = ""
        Me.txtProductCode.Text = ""

        Me.txtProductCode.Focus()

    End Sub
    Private Sub txtQty_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtQty.TextChanged, txtPrice.TextChanged
        Dim amount As Double = Val(Me.txtPrice.Text) * Val(Me.txtQty.Text)
        Me.txtAmount.Text = amount.ToString("#,##0.00") 
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        BrowseProduct()
    End Sub
    Private Sub BrowseProduct()
        Dim frm As New FormProductBrowser()
        frm.txtSearch.Text = Me.txtProductCode.Text
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim row = frm.dgv.SelectedRows(0)
            Me.txtProductId.Text = row.Cells("ProductId").Value
            Me.txtProductCode.Text = row.Cells("ProductCode").Value
            Me.txtProductName.Text = row.Cells("ProductName").Value
            Me.txtPrice.Text = row.Cells("Price").Value
            Me.txtQty.Text = "1"
            Me.txtAmount.Text = Me.txtPrice.Text
        End If
    End Sub

    Private Sub btnAddLine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddLine.Click
        '' Me.dgv.Rows.Add(Me.txtProductId.Text, Me.txtProductCode.Text, Me.txtProductName.Text, Me.txtPrice.Text, Me.txtQty.Text, Me.txtAmount.Text)
        Dim searchRow = FindProductInList(Me.txtProductId.Text)

        If searchRow Is Nothing Then
            Dim i = Me.dgv.Rows.Add()
            Dim row = Me.dgv.Rows(i)
            row.Cells("ProductId").Value = Me.txtProductId.Text
            row.Cells("ProductCode").Value = Me.txtProductCode.Text
            row.Cells("ProductName").Value = Me.txtProductName.Text
            row.Cells("Price").Value = Val(Me.txtPrice.Text)
            row.Cells("Qty").Value = Val(Me.txtQty.Text)
            row.Cells("Amount").Value = Val(Me.txtAmount.Text)
        Else
            searchRow.Cells("Qty").Value = searchRow.Cells("Qty").Value + Val(Me.txtQty.Text)
            searchRow.Cells("Price").Value = Me.txtPrice.Text
            searchRow.Cells("Amount").Value = Val(searchRow.Cells("Qty").Value) * Me.txtPrice.Text
        End If 
        ClearLine() 
    End Sub
    Private Function FindProductInList(ByVal id As Int16) As DataGridViewRow 
        For Each row As DataGridViewRow In Me.dgv.Rows
            If row.Cells("ProductId").Value = id Then
                Return row
            End If
        Next
        Return Nothing
    End Function
End Class